package com.shangcheng.controller;

import com.shangcheng.common.Result;
import com.shangcheng.entity.TCustomer;
import com.shangcheng.service.CustmerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/public")//public开头的都不检查登陆
@RestController
@CrossOrigin("*")
public class PublicCustomerController {
    @Autowired
    private CustmerServiceImpl service;
    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody Map<String,String> loginMap){
        Result<Map<String,Object>>  re = Result.ok(null);
        String userName = loginMap.get("username");
        String passWord = loginMap.get("password");
        TCustomer retCustmer = service.queryByUsername(userName);
        if(retCustmer==null){
            re.setCode(100);
            re.setMessage("用户名错误！");
        }else{
            // if(!retCustmer.getPassword().equals(SecureUtil.md5(passWord))){
            if(!retCustmer.getPassword().equals(passWord)){
                re.setCode(101);
                re.setMessage("密码错误！");
            } else {
                Map<String,Object> map = new HashMap<>();
                map.put("name",retCustmer.getName());
                map.put("id",retCustmer.getId());
                map.put("username",retCustmer.getUserName());
                map.put("token",service.createTocken(retCustmer));
                re.setData(map);
                re.setMessage("登陆成功！");
            }
        }
        return re;
    }
}
